package com.example.jie.controll;

import com.example.jie.tool.MinioConfig;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.omg.CORBA.portable.OutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("upload")
public class UploadFileControll {
    @Autowired
    private MinioClient minioClient;
    //多文件分片上传到文件夹
//    @RequestMapping("addFile")
//    public ResponseEntity<?> addFile(
//            @RequestParam("file")MultipartFile file,
//            @RequestParam("fileName") String fileName,
//            @RequestParam("chunkNumber") Integer chunkNumber,
//            @RequestParam("totalChunks") Integer totalChunks
//            ) throws IOException {
//
//        File fileUrl=  new File("D:/a大三下实习/ge实习/split_upload_end/project/test1");
//        if (!fileUrl.exists()){
//            fileUrl.mkdirs();
//        }
//        File file1 = new File(fileUrl,fileName+"-"+chunkNumber);
//        file.transferTo(file1);
//        if (totalChunks.equals(chunkNumber)){
//            String destPath = "D:/a大三下实习/ge实习/split_upload_end/project/test2/" + fileName;
//            FileOutputStream fileOutputStream = new FileOutputStream(destPath);
//            for (int i = 1 ; i <=totalChunks ;i++){
//                File chunk = new File(fileUrl,fileName+"-"+i);
//                FileInputStream fileInputStream  = new FileInputStream(chunk);
//                byte[] bytes = new byte[1024];
//                int len;
//                while ((len =fileInputStream.read(bytes))!=-1){
//                    fileOutputStream.write(bytes,0,len);
//                }
//                fileInputStream.close();
//                chunk.delete();
//            }
//            fileOutputStream.close();
//        }
//        return ResponseEntity.ok().build();
//
//    }


    //多文件上传到minio
    @RequestMapping("addFile")
    public ResponseEntity<?> addFile(
            @RequestParam("file")MultipartFile file,
            @RequestParam("fileName") String fileName,
            @RequestParam("chunkNumber") Integer chunkNumber,
            @RequestParam("totalChunks") Integer totalChunks
    ) throws IOException {

        File fileUrl=  new File("D:/a大三下实习/ge实习/split_upload_end/project/test1");
        if (!fileUrl.exists()){
            fileUrl.mkdirs();
        }
        File file1 = new File(fileUrl,fileName+"-"+chunkNumber);
        file.transferTo(file1);
        if (totalChunks.equals(chunkNumber)){
            String destPath = "D:/a大三下实习/ge实习/split_upload_end/project/test2/" + fileName;
            FileOutputStream fileOutputStream = new FileOutputStream(destPath);
            for (int i = 1 ; i <=totalChunks ;i++){
                File chunk = new File(fileUrl,fileName+"-"+i);
                FileInputStream fileInputStream  = new FileInputStream(chunk);
                byte[] bytes = new byte[1024*1024];
                int len;
                while ((len =fileInputStream.read(bytes))!=-1){
                    fileOutputStream.write(bytes,0,len);
                }
                FileInputStream fileInputStream1 = new FileInputStream(destPath);
                File file2 = new File(destPath);
                try {
                    minioClient.putObject(PutObjectArgs.builder().bucket("upload-file").object(fileName)
                            .stream(fileInputStream1,file2.length(),-1).contentType(file.getContentType()).build());
                } catch (ErrorResponseException e) {
                    e.printStackTrace();
                } catch (InsufficientDataException e) {
                    e.printStackTrace();
                } catch (InternalException e) {
                    e.printStackTrace();
                } catch (InvalidBucketNameException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (InvalidResponseException e) {
                    e.printStackTrace();
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (ServerException e) {
                    e.printStackTrace();
                } catch (XmlParserException e) {
                    e.printStackTrace();
                }

                fileInputStream.close();
                chunk.delete();
            }
            fileOutputStream.close();
        }
        return ResponseEntity.ok().build();

    }



}
